/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package east2west.dao;

import east2west.dto.TourServiceDTO;
import east2west.interfaces.TourServiceInterface;
import east2west.util.DatabaseUtil;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Admin
 */
public class TourServiceDAO implements TourServiceInterface {

    Connection con = null;
    CallableStatement call = null;
    ResultSet rs = null;

    public ArrayList<TourServiceDTO> readByAl() {
        try {
            con = DatabaseUtil.getConnection();
            ArrayList<TourServiceDTO> list = new ArrayList<TourServiceDTO>();
            call = con.prepareCall("select * from V_TourService_Info");
            rs = call.executeQuery();
            while (rs.next()) {
                TourServiceDTO dto = new TourServiceDTO();
                dto.setTourServiceId(rs.getInt(1));
                dto.setTourServiceName(rs.getString(2));
                dto.setDescription(rs.getString(3));
                list.add(dto);
            }
            return list;
        } catch (Exception ex) {
            return null;

        } finally {
            DatabaseUtil.closeConnection(con);
        }

    }

    public ArrayList<TourServiceDTO> findByName(String tourServiceName) {
        try {
            con = DatabaseUtil.getConnection();
            ArrayList<TourServiceDTO> list = new ArrayList<TourServiceDTO>();
            call = con.prepareCall("{call sp_Call_View_TourService_FindName(?)}");
            call.setString(1, tourServiceName);
            rs = call.executeQuery();
            while (rs.next()) {
                TourServiceDTO dto = new TourServiceDTO();

                dto.setTourServiceId(rs.getInt(1));
                dto.setTourServiceName(rs.getString(2));
                dto.setDescription(rs.getString(3));
                list.add(dto);
            }
            return list;
        } catch (Exception ex) {
            return null;

        }
    }

    public ArrayList<TourServiceDTO> findByTourServiceDto(TourServiceDTO dto) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public TourServiceDTO findById(int id) {
        try {
            con = DatabaseUtil.getConnection();
            TourServiceDTO dto = null;
            call = con.prepareCall("{call sp_Call_View_TourService_FindId(?)}");
            call.setInt(1, id);
            rs = call.executeQuery();
            while (rs.next()) {
                dto = new TourServiceDTO();
                dto.setTourServiceId(rs.getInt(1));
                dto.setTourServiceName(rs.getString(2));
                dto.setDescription(rs.getString(3));
            }
            return dto;
        } catch (Exception ex) {
            return null;

        }
    }

    public boolean create(TourServiceDTO dto) {
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall("{call sp_Insert_TourService(?,?)}");
            call.setString(1, dto.getTourServiceName());
            call.setString(2, dto.getDescription());
            call.executeUpdate();
            return true;
        } catch (SQLException ex) {
            return false;

        } finally {
            try {
                if (con != null && !con.isClosed()) {
                    DatabaseUtil.closeConnection(con);
                }
            } catch (SQLException ex) {
            }
        }
    }

    public boolean update(TourServiceDTO dto) {
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall("{call sp_Update_TourService(?,?,?)}");
            call.setInt(1, dto.getTourServiceId());
            call.setString(2, dto.getTourServiceName());
            call.setString(3, dto.getDescription());
            call.executeUpdate();

            return true;
        } catch (SQLException ex) {
            return false;

        } finally {
            try {
                if (con != null && !con.isClosed()) {
                    DatabaseUtil.closeConnection(con);
                    call.close();
                }
            } catch (SQLException ex) {
            }
        }
    }

    public boolean delete(TourServiceDTO dto) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean delete(int id) {
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall("{call sp_Delete_TourService(?)}");
            call.setInt(1, id);
            call.executeUpdate();
            return true;
        } catch (SQLException ex) {
            return false;

        } finally {
            try {
                if (con != null && !con.isClosed()) {
                    DatabaseUtil.closeConnection(con);
                }
            } catch (SQLException ex) {
            }
        }
    }
}
